package com.yuan.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuan.Mapper.OrderMapper;
import com.yuan.Mapper.UserMapper;
import com.yuan.Service.ReportService;
import com.yuan.Service.WorkspaceService;
import com.yuan.entity.Orders;
import com.yuan.entity.User;
import com.yuan.vo.TurnoverReportVO;
import com.yuan.vo.UserReportVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private WorkspaceService workspaceService;

    @Override
    public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {
        //当前集合用于存放从begin到end范围内的每天的日期
        List<LocalDate> dateList = new ArrayList<>();

        dateList.add(begin);

        while (!begin.equals(end)) {
            //日期计算，计算指定日期的后一天对应的日期
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        //存放每天的营业额
        List<Double> turnoverList = new ArrayList<>();
        for (LocalDate date : dateList) {
            //查询date日期对应的营业额数据，营业额是指：状态为“已完成”的订单金额合计
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);


            double count = orderMapper.selectCount(new LambdaQueryWrapper<Orders>().in(Orders::getOrderTime, beginTime, endTime)
                    .eq(Orders::getStatus, Orders.COMPLETED));
            Double v = Double.valueOf(count);

            v = v == null ? 0.0 : v;

            turnoverList.add(v);
        }
        TurnoverReportVO build = TurnoverReportVO.builder().dateList(StringUtils.join(dateList, ",")).turnoverList(
                StringUtils.join(turnoverList, ",")
        ).build();
        return build;
    }

    @Override
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        //存放从begin到end之间的每天对应的日期
        List<LocalDate> dateList = new ArrayList<>();

        dateList.add(begin);

        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        //存放每天的新增用户数量 select count(id) from user where create_time < ? and create_time > ?
        List<Integer> newUserList = new ArrayList<>();
        List<Integer> totalUserList = new ArrayList<>();


        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            Map map = new HashMap();
            map.put("end", endTime);

            //总用户数量
            Long totalUser = userMapper.selectCount(new LambdaQueryWrapper<User>()
                    .le(User::getCreateTime, endTime)
            );


        }
        return new UserReportVO();
    }


}
